package com.icoolkj.ms.user.controller;

import cn.hutool.core.util.StrUtil;
import com.icoolkj.ms.api.auth.bo.UserInfoInTokenBO;
import com.icoolkj.ms.common.core.enums.UserTypeEnum;
import com.icoolkj.ms.common.core.enums.AdminFlagEnum;
import com.icoolkj.ms.api.auth.feign.AccountRegisterFeignClient;
import com.icoolkj.ms.api.auth.vo.TokenInfoVO;
import com.icoolkj.ms.common.core.response.ServerResponseEntity;
import com.icoolkj.ms.common.core.utils.PrincipalUtil;
import com.icoolkj.ms.user.dto.UserRegisterDTO;
import com.icoolkj.ms.user.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author icoolkj
 * @version 1.0
 * @description 用户注册
 * @createDate 2025/02/19 10:02
 */
@Tag(name = "用户注册")
@RequestMapping("/public/user/register")
@RestController
public class UserRegisterController {

    @Autowired
    private IUserService iUserService;

    @Autowired
    private AccountRegisterFeignClient accountFeignClient;

    @Operation(summary = "APP-用户注册")
    @PostMapping("/app")
    public ServerResponseEntity<TokenInfoVO> appUserRegister(@Valid @RequestBody UserRegisterDTO userRegisterDTO) {

        // 检查 APP 端必填项
        ServerResponseEntity<String> requiredCheckResult = checkAppRequiredFields(userRegisterDTO);
        if (!requiredCheckResult.isSuccess()) {
            return ServerResponseEntity.transform(requiredCheckResult);
        }

        // 规则检查
        ServerResponseEntity<String> ruleCheckResult = this.checkRegisterInfo(userRegisterDTO);
        if (!ruleCheckResult.isSuccess()) {
            return ServerResponseEntity.transform(ruleCheckResult);
        }

        // 设置昵称
        if (StrUtil.isBlank(userRegisterDTO.getNickName())) {
            userRegisterDTO.setNickName(userRegisterDTO.getPhoneNumber());
        }

        return saveUserAndLogin(userRegisterDTO);
    }

    @Operation(summary = "WEB-用户注册")
    @PostMapping("/web")
    public ServerResponseEntity<TokenInfoVO> webUserRegister(@Valid @RequestBody UserRegisterDTO userRegisterDTO) {
        // 检查 WEB 端必填项
        ServerResponseEntity<String> requiredCheckResult = checkWebRequiredFields(userRegisterDTO);
        if (!requiredCheckResult.isSuccess()) {
            return ServerResponseEntity.transform(requiredCheckResult);
        }

        // 规则检查
        ServerResponseEntity<String> ruleCheckResult = this.checkRegisterInfo(userRegisterDTO);
        if (!ruleCheckResult.isSuccess()) {
            return ServerResponseEntity.transform(ruleCheckResult);
        }

        // 设置昵称
        if (StrUtil.isBlank(userRegisterDTO.getNickName())) {
            userRegisterDTO.setNickName(userRegisterDTO.getUserName());
        }

        return saveUserAndLogin(userRegisterDTO);
    }

    /**
     * 检查 APP 端必填项
     * @param userRegisterDTO 用户注册信息
     * @return 检查结果
     */
    private ServerResponseEntity<String> checkAppRequiredFields(UserRegisterDTO userRegisterDTO) {
        if (StrUtil.isBlank(userRegisterDTO.getPhoneNumber())) {
            return ServerResponseEntity.fail("APP 端注册必须提供手机号码");
        }
        return ServerResponseEntity.success();
    }

    /**
     * 检查 WEB 端必填项
     * @param userRegisterDTO 用户注册信息
     * @return 检查结果
     */
    private ServerResponseEntity<String> checkWebRequiredFields(UserRegisterDTO userRegisterDTO) {
        if (StrUtil.isBlank(userRegisterDTO.getUserName())) {
            return ServerResponseEntity.fail("WEB 端注册必须提供用户名");
        }
        if (StrUtil.isBlank(userRegisterDTO.getPhoneNumber())) {
            return ServerResponseEntity.fail("WEB 端注册必须提供手机号码");
        }
        if (StrUtil.isBlank(userRegisterDTO.getEmail())) {
            return ServerResponseEntity.fail("WEB 端注册必须提供邮箱");
        }
        return ServerResponseEntity.success();
    }

    /**
     * 保存用户信息并登录
     * @param userRegisterDTO 用户注册信息
     * @return 登录后的 Token 信息
     */
    private ServerResponseEntity<TokenInfoVO> saveUserAndLogin(UserRegisterDTO userRegisterDTO) {
        // 1. 保存账户信息
        Long uid = iUserService.saveUserRegister(userRegisterDTO);
        // 2. 登录
        UserInfoInTokenBO userInfoInTokenBO = new UserInfoInTokenBO();
        userInfoInTokenBO.setUid(uid);
        userInfoInTokenBO.setUserId(userRegisterDTO.getUserId());
        userInfoInTokenBO.setUserType(UserTypeEnum.NORMAL_USER.getCode());
        userInfoInTokenBO.setIsAdmin(AdminFlagEnum.NORMAL.getCode());
        return accountFeignClient.storeTokenAndGetVo(userInfoInTokenBO);
    }

    private ServerResponseEntity<String> checkRegisterInfo(UserRegisterDTO userRegisterDTO) {
        // 用户名
        if (StrUtil.isNotBlank(userRegisterDTO.getUserName()) && !PrincipalUtil.isUserName(userRegisterDTO.getUserName())) {
            return ServerResponseEntity.fail("用户名格式不正确");
        }
        // 手机号
        if (StrUtil.isNotBlank(userRegisterDTO.getPhoneNumber()) && !PrincipalUtil.isMobile(userRegisterDTO.getPhoneNumber())) {
            return ServerResponseEntity.fail("手机号格式不正确");
        }
        // 邮箱
        if (StrUtil.isNotBlank(userRegisterDTO.getEmail()) && !PrincipalUtil.isEmail(userRegisterDTO.getEmail())) {
            return ServerResponseEntity.fail("邮箱格式不正确");
        }
        return ServerResponseEntity.success();
    }

}
